package cn.learn.controller.cargo;

import cn.learn.base.BaseController;
import cn.learn.domain.cargo.Contract;
import cn.learn.domain.cargo.ContractExample;
import cn.learn.service.cargo.ContractService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/cargo/contract")
public class ContractController extends BaseController {

    @Reference
    private ContractService contractService;

    /**
     * 分页查询全部合同
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", name = "查看购销合同")
    public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        ContractExample contractExample = new ContractExample();
        // 根据合同时间排序
        contractExample.setOrderByClause("create_time desc");
        // 设置条件
        ContractExample.Criteria criteria = contractExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId); // 设置对应的公司编号，根据公司找

//        //细粒度的权限控制（根据当前用户的不同级别 展示不同的数据）
        if(user.getDegree()==4){
            // 按照创建人去查
            criteria.andCreateByEqualTo(user.getId());
        }else if(user.getDegree()==3){
            criteria.andCreateDeptEqualTo(user.getDeptId());
        }else if(user.getDegree()==2){
            criteria.andCreateDeptLike(user.getDeptId()+"%"); //前提是部门字段要有逻辑
        }

        PageInfo resultList = contractService.findAll(contractExample, pageNum, pageSize);
        request.setAttribute("pageList", resultList);
        return "/cargo/contract/contract-list";
    }

    /**
     * 添加新合同页面 与 跳转页面
     * @return
     */
    @RequestMapping(value = "/addOne", name = "添加一条合同")
    public String addOne(Contract contract) {
        if (request.getMethod().equals("POST")) {
            contract.setCompanyId(companyId);
            contract.setCompanyName(companyName);
            contract.setCreateBy(user.getId()); //创建人
            contract.setCreateDept(user.getDeptId()); //创建部门
            contractService.save(contract);
            return "redirect:/cargo/contract/list.do";
        }
        return "/cargo/contract/contract-add";
    }

    /**
     * 跳转到查看页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/toView", name = "跳转查看页面")
    public String toView(String id) {
        Contract resultData = contractService.findById(id);
        request.setAttribute("data", resultData);
        return "cargo/contract/contract-view";
    }

    /**
     * 跳转到修改页面 与 修改内容
     * @param contract
     * @return
     */
    @RequestMapping(value = "/updateById", name = "修改一条合同")
    public String updateById(Contract contract) {
        if (request.getMethod().equals("POST")) {
            contract.setCompanyId(companyId);
            contract.setCompanyName(companyName);
            contractService.update(contract);
            return "redirect:/cargo/contract/list.do";
        }
        Contract resultData = contractService.findById(contract.getId());
        request.setAttribute("data", resultData);
        return "cargo/contract/contract-update";
    }

    /**
     * 根据id删除一个合同
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", name = "删除合同")
    public String delete(String id) {
        contractService.delete(id);
        return "redirect:/cargo/contract/list.do";
    }

    /**
     * 合同提交
     * @param id
     * @return
     */
    @RequestMapping(value = "/submit", name = "提交合同")
    public String submit(String id) {
        Contract contract = contractService.findById(id);
        contract.setState(1);
        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }

    /**
     * 合同取消提交
     * @param id
     * @return
     */
    @RequestMapping(value = "/cancel", name = "合同取消提交")
    public String cancel(String id) {
        Contract contract = contractService.findById(id);
        contract.setState(0);
        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }

}
